#!/usr/bin/env bash
#####################
install_yay_main() {
    case $1 in
    -bf | --build-fakeroot) build_fakeroot ;;
    --add-archlinuxcn-repo) add_archlinuxcn_repo ;;
    --archlinux-repo-mirror) add_archlinuxcn_repo_mirror ;;
    -yay | --install-yay) install_yay ;;
    esac
}
build_fakeroot() {
    for i in make automake autoconf gcc libtool po4a patch; do
        if [[ ! $(command -v ${i}) ]]; then
            printf "%s\n" "${GREEN}pacman ${YELLOW}-Sy --noconfirm ${BLUE}${i}${RESET}"
            pacman -Sy --noconfirm ${i} || pacman -Syu --noconfirm ${i}
        fi
    done
    [[ ! $(command -v fakeroot) ]] || pacman -R --noconfirm fakeroot
    [[ $(command -v whereis) ]] || pacman -S --noconfirm util-linux
    [[ $(command -v whiptail) ]] || pacman -S --noconfirm libnewt
    unset FAKEROOT_SRC_VERSION
    FAKEROOT_SRC_REPO='https://ftp.debian.org/debian/pool/main/f/fakeroot/'
    FAKEROOT_SRC_REPO_02='https://mirrors.bfsu.edu.cn/debian/pool/main/f/fakeroot/'
    FAKEROOT_SRC_VERSION=$(curl -L ${FAKEROOT_SRC_REPO} | grep \.orig\.tar | tail -n 1 | awk -F '<a href=' '{print $2}' | cut -d '"' -f 2)
    [[ -n ${FAKEROOT_SRC_VERSION} ]] || FAKEROOT_SRC_VERSION=$(curl -L ${FAKEROOT_SRC_REPO_02} | grep \.orig\.tar | tail -n 1 | awk -F '<a href=' '{print $2}' | cut -d '"' -f 2)
    FAKEROOT_SRC_URL="${FAKEROOT_SRC_REPO}${FAKEROOT_SRC_VERSION}"
    FAKEROOT_SRC_URL_02="${FAKEROOT_SRC_REPO_02}${FAKEROOT_SRC_VERSION}"
    FAKEROOT_SRC_FILE=$(printf "%s\n" ${FAKEROOT_SRC_VERSION} | sed 's@fakeroot_@fakeroot-@g')
    cd /tmp
    aria2c --console-log-level=info --no-conf --allow-overwrite=true -s 5 -x 5 -k 1M -o ${FAKEROOT_SRC_FILE} ${FAKEROOT_SRC_URL} || curl -Lv -o ${FAKEROOT_SRC_FILE} ${FAKEROOT_SRC_URL_02}
    tar -xvf ${FAKEROOT_SRC_FILE}
    cd ${FAKEROOT_SRC_FILE%.*.*.*}
    ./bootstrap
    ./configure --prefix=/usr --libdir=/usr/lib/libfakeroot --with-ipc=tcp --disable-static
    make -j4
    cd doc
    po4a -k 0 --rm-backups --variable "srcdir=../doc/" po4a/po4a.cfg
    cd ..
    make install
    cd /tmp
    rm -rv ${FAKEROOT_SRC_FILE%.*.*.*} ${FAKEROOT_SRC_FILE}
    cd ${HOME}
    [[ $(command -v whiptail) ]] || pacman -S --noconfirm libnewt
}
add_archlinuxcn_repo() {
    cp -fv /etc/pacman.conf /etc/pacman.conf.bak
    if ! grep -q 'archlinuxcn' /etc/pacman.conf; then
        cat >>/etc/pacman.conf <<-'Endofpacman'
					[archlinuxcn]
					Server = https://repo.archlinuxcn.org/$arch
					Server = https://mirror.xtom.com/archlinuxcn/$arch
					Server = https://mirrors.ocf.berkeley.edu/archlinuxcn/$arch
					Server = https://mirror.xtom.nl/archlinuxcn/$arch
					Server = https://opentuna.cn/archlinuxcn/$arch
					Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch
					Server = https://mirrors.bfsu.edu.cn/archlinuxcn/$arch
					SigLevel = Never
				Endofpacman
    fi
    for i in archlinux-keyring archlinuxcn-keyring; do
        printf "%s\n" "${GREEN}pacman ${YELLOW}-Sy --noconfirm ${BLUE}${i}${RESET}"
        pacman -Sy --noconfirm ${i} || pacman -Syu --noconfirm ${i}
    done
}
add_archlinuxcn_repo_mirror() {
    if [[ -s /etc/pacman.conf.bak ]]; then
        cp -fv /etc/pacman.conf.bak /etc/pacman.conf
        cat >>/etc/pacman.conf <<-'Endofpacman'
					[archlinuxcn]
					Server = https://mirrors.bfsu.edu.cn/archlinuxcn/$arch                    
					Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch
					Server = https://opentuna.cn/archlinuxcn/$arch
					Server = https://repo.archlinuxcn.org/$arch
					Server = https://mirror.xtom.com/archlinuxcn/$arch
					Server = https://mirrors.ocf.berkeley.edu/archlinuxcn/$arch
					Server = https://mirror.xtom.nl/archlinuxcn/$arch                                        
					SigLevel = Never
				Endofpacman
    fi
}
install_yay() {
    for i in yay gawk; do
        printf "%s\n" "${GREEN}pacman ${YELLOW}-Sy --noconfirm ${BLUE}${i}${RESET}"
        pacman -Sy --noconfirm ${i} || pacman -Syu --noconfirm ${i} || pacman -Sy --noconfirm ${i}
    done
}
###################
install_yay_main $@
